var express = require('express');
var md5 = require('./../tools/md5.js')
var AES =require('./../tools/AES.js')
var router = express.Router();

var db = require('../sqldb/index.js');
var nurse =require('../model/nurse.js')

/* GET users listing. */
router.post('/', function(req, res, next) {
	return db.sequelize.transaction(function(t){
		console.log("+++++++++++++++++++")
		return nurse(db.sequelize).findOne({
			where:{
				nUserNum:req.body.nUserNum
			}
		},{
			transaction:t
		}).then(function(data){
			if (req.body.nPassword==data.nPassword) {
				res.cookie('hasLogin', md5(1), { 
					expires: new Date(Date.now() + 10000 * 60 * 60 * 24 * 0.25)
				});
				res.cookie('nUserNum',AES.aesKey(data.nUserNum), { 
					expires: new Date(Date.now() + 10000 * 60 * 60 * 24 * 0.25)
				})
				res.cookie('dName',AES.aesKey(data.nDeptBelong), { 
					expires: new Date(Date.now() + 10000 * 60 * 60 * 24 * 0.25)
				})
				res.json({
					status:1,
					msg:'',
					result:data
				})
			}else{
				res.json({
					status:0,
					msg:'',
					result:''
				})
			}
		}).catch(function(err){
			console.log("发生错误：" + err);
		});
	})
});

router.post('/out', function(req, res, next) {
	res.clearCookie('hasLogin');
	res.clearCookie('nUserNum');
	res.clearCookie('dName');
	res.json({
		status:1,
		msg:'',
		result:''
	})
});


router.get('/add', function(req, res, next) {
	return db.sequelize.transaction(function(t){
		console.log("+++++++++++++++++++")
		return nurse(db.sequelize).create({
			nName:'邢力',
			nUserNum:'15256909020',
			nPassword:md5('xingli'),
			nSex:'男',
			nDeptBelong:'耳鼻喉科'
		},{
			transaction:t
		}).then(function(data){
			res.json({
				status:1,
				msg:'',
				result:data
			})
		}).catch(function(err){
			res.json({
				status:0,
				msg:err,
				result:''
			})
			console.log("发生错误：" + err);
		});
	})
});
module.exports = router;
